package com.speed.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.speed.controller.ActiviterController;
import com.speed.controller.AdminController;
import com.speed.controller.FileController;
import com.speed.controller.QrCodeController;
import com.speed.controller.SeatController;
import com.speed.controller.SmsController;
import com.speed.controller.UserController;
import com.speed.controller.VerifyCodeController;
import com.speed.interceptor.GlobalActionInterceptor;
import com.speed.interceptor.GlobalServiceInterceptor;
import com.speed.plugs.shiro.ShiroInterceptor;
import com.speed.plugs.shiro.ShiroPlugin;

public class BaseConfig extends JFinalConfig {

	private static final String CONFIG_NAME = "speed_config.txt";

	private final String jdbc_url;
	private final String jdbc_user;
	private final String jdbc_password;
	private final Boolean dev_mode;
	private ShiroPlugin shiroPlugin = null;

	public BaseConfig() {
		PropKit.use(CONFIG_NAME);
		jdbc_url = PropKit.get(Constant.JDBC_URL_KEY);
		jdbc_user = PropKit.get(Constant.JDBC_USER_KEY);
		jdbc_password = PropKit.get(Constant.JDBC_PASSWORD_KEY);
		dev_mode = PropKit.getBoolean(Constant.DEV_MODE);
		//只需要使用  Shiro默认 realm 即可 ，当前系统并不需要太复杂的权限校验
		IniSecurrityManager.INSTANCE.setOtherIniConfigByFilePath("shiro.ini");
	}

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(dev_mode);
		me.setViewType(ViewType.JSP);
	}

	@Override
	public void configHandler(Handlers handlers) {
	}

	@Override
	public void configInterceptor(Interceptors interceptors) {
		for (String rex : Constant.TX_REGEX_ARY) {
			interceptors.add(new TxByRegex(rex));
		}
		interceptors.addGlobalActionInterceptor(new GlobalActionInterceptor());
		interceptors.addGlobalServiceInterceptor(new GlobalServiceInterceptor());
		// interceptors.add(new ActivitesInterceptor());
		//全局shiro拦截--只会拦截带有 shiro注解的 Controller
		//interceptors.add(new ShiroInterceptor());
	}

	@Override
	public void configPlugin(Plugins plugins) {

		C3p0Plugin c3p0Plugin = new C3p0Plugin(jdbc_url, jdbc_user, jdbc_password);
		plugins.add(c3p0Plugin);

		// 自动扫描所有实体类的表名
		AutoTableBindPlugin tableBindPlugin = new AutoTableBindPlugin(c3p0Plugin);
		tableBindPlugin.addScanPackages("com/speed/entity");
		plugins.add(tableBindPlugin);

		//添加shiro插件
		plugins.add(shiroPlugin);
	}

	@Override
	public void configRoute(Routes routes) {
		routes.add("/user", UserController.class, "/WEB-INF/user");
		routes.add("/file", FileController.class, "/WEB-INF/file");
		routes.add("/activiter", ActiviterController.class, "/WEB-INF/activiter");
		routes.add("/seat", SeatController.class, "/WEB-INF/seat");
		routes.add("/admin", AdminController.class, "/WEB-INF/admin");
		routes.add("/sms", SmsController.class, "/WEB-INF/sms");
		routes.add("/verify", VerifyCodeController.class);
		routes.add("/qr", QrCodeController.class);
		shiroPlugin = new ShiroPlugin(routes);
	}

}
